package com.lovo.ssc.service.impl;


import com.lovo.dto.PendingCommodityDto;
import com.lovo.ssc.entity.SysGoods;
import com.lovo.ssc.service.IShelvesService;
import com.lovo.ssc.service.ISysGoods2Service;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class PendingListen {
    @Autowired
    IShelvesService service;
    @Autowired
    ISysGoods2Service sysGoods2Service;

    @RabbitListener(queues = MQUtil.pendingCommodityQueue)
    public void update(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, PendingCommodityDto dto)
            throws IOException, InterruptedException{
        if(dto != null){
            SysGoods goods = new SysGoods();
            goods.setGoodsName(dto.getCommodityname());
            goods.setSellPrice(dto.getPrice().floatValue());
            goods.setCode(dto.getCommodityCode());
            service.update(goods.getCode(),goods.getSellPrice());
            SysGoods good = service.findGoodsByCode(goods.getCode());
            service.upGoodsStatue(good.getCode(),"1");
            sysGoods2Service.updatePrice(dto.getCommodityCode(),dto.getPrice());
        }
        channel.basicAck(tag,false); //false不进行批量确认
    }
}